﻿/**《Version、Commend Info》************************************************
 * FileName：TestHelper
 * Function：
 * Description：
 * Version：V2.0.1
 * ------------------------------------------
 * CreateTime：2021/10/28 10:42:04
 * Author：赵旋
 * Email： 1192508693@qq.com
 * ------------------------------------------
 * UserName：   DanyMoon
 * MachineName：DESKTOP-S78OH2C
 * ========================================================================= 
 * 
 * Modifyier：	
 * ModifyTime：	
 * Description：	
 ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Common.Base.Test;
using System.Configuration;

namespace IntershipProject
{
    public class TestHelper
    {
        public static Dictionary<TestClsEnum.TestClsEnumX, ITest> TestInstances = new Dictionary<TestClsEnum.TestClsEnumX, ITest>();
        public static Dictionary<TestClsEnum.TestClsEnumX, string> AssemblyNames = new Dictionary<TestClsEnum.TestClsEnumX, string>();
        public static TestClsEnum.TestClsEnumX CurrCls = TestClsEnum.TestClsEnumX.Example;
        public static ITest CurrInstance
        {
            get { return TestInstances[CurrCls]; }
        }
        static TestHelper()
        {
            InitTestInstances();
            InitCurrCls();
        }

        //取值
        private static void InitTestInstances()
        {
            var testClsNames = ConfigurationManager.AppSettings.AllKeys.Where(name => name.StartsWith("TestCls")).ToList();
            foreach (var testClsName in testClsNames)
            {
                AssemblyNames.Add(TestClsEnum.GetKey(testClsName.Split('_')[1]), ConfigurationManager.AppSettings[testClsName]);
                TestInstances.Add(TestClsEnum.GetKey(testClsName.Split('_')[1]), GetInstance(ConfigurationManager.AppSettings[testClsName]));
            }
        }

        private static void InitCurrCls()
        {
            CurrCls = TestClsEnum.GetKey(ConfigurationManager.AppSettings["CurrTestCls"].Split('_')[1]);
        }

        public static void RefreshCurrCls(TestClsEnum.TestClsEnumX currCls)
        {
            CurrCls = currCls;
            SetAppSettings(string.Format("TestCls_{1}", Enum.GetName((typeof(TestClsEnum.TestClsEnumX)), currCls)), AssemblyNames[currCls]);
        }

        private static ITest GetInstance(string assemblyName)
        {
            if (string.IsNullOrEmpty(assemblyName))
            {
                return null;
            }
            System.Reflection.Assembly asm =
 System.Reflection.Assembly.LoadFrom(assemblyName + ".dll");
            var classType = (from p in asm.GetTypes()
                             let attributeItem = p.GetCustomAttributes(true).Where(y => y.GetType() == typeof(TestRegisterAttribute)).FirstOrDefault()
                             where attributeItem != null
                             select p).FirstOrDefault();
            if (classType == null)
            {
                return null;
            }

            return (ITest)asm.CreateInstance(classType.FullName);
        }

        private static void SetAppSettings(string configName, string value)
        {

            bool isModified = false;
            if (ConfigurationManager.AppSettings.AllKeys.Any(p => p == configName))
            {
                isModified = true;
            }

            string file = System.Environment.CurrentDirectory;
            Configuration config = ConfigurationManager.OpenExeConfiguration(file);
            if (isModified)
            {
                config.AppSettings.Settings.Remove(configName);
            }
            config.AppSettings.Settings.Add(configName, value);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}